package com.rohoo.user.dao.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.directwebremoting.util.Logger;
import org.springframework.dao.EmptyResultDataAccessException;

import com.rohoo.framework.dao.BaseDAO;
import com.rohoo.framework.dao.DAORowMapper;
import com.rohoo.framework.util.StringUtil;
import com.rohoo.user.bo.Register;
import com.rohoo.user.dao.RegisterDAO;
import com.rohoo.user.dto.RegisterDTO;

public class RegisterDAOImpl  extends BaseDAO implements RegisterDAO{
	Logger log = Logger.getLogger(this.getClass());
	public int selUserId() throws Exception {
		String sql="select max(user_id) from td_user";
		Integer result=getJdbcTemplate().queryForInt(sql);
		if(result==0){
			result=1;
		}else{
			result=result+1;
		}
		return result;
	}

	public boolean add(Register register) throws Exception{
		String sql = "";
		try {
			String userpwd = DigestUtils.md5Hex(register.getUserpwd());
			sql = "insert into USER (user_id,useridcard,username,userpwd,usersex," +
					"userbirth,usertel,usermobile,useraddress,usermail,usercomp) values " + 
					"('"+register.getUser_id()+"',"+
					"'"+StringUtil.filterForSQL(register.getUseridcard())+"',"+
					"'"+StringUtil.filterForSQL(register.getUsername())+"',"+
					"'"+StringUtil.filterForSQL(userpwd)+"',"+
					"'"+StringUtil.filterForSQL(register.getUsersex())+"',"+
					"to_date('" + register.getUserbirth() + "','yyyy-MM-dd'),"+
					"'"+StringUtil.filterForSQL(register.getUsertel())+"',"+
					"'"+StringUtil.filterForSQL(register.getUsermobile())+"',"+
					"'"+StringUtil.filterForSQL(register.getUseraddress())+"',"+
					"'"+StringUtil.filterForSQL(register.getUsermail())+"',"+
					"'"+StringUtil.filterForSQL(register.getUsercomp())+"')";
			//System.out.println(sql);
			this.getJdbcTemplate().update(sql);
		} catch (Exception e) {
			System.out.println(e);
			log.error("add Error! sql=" + sql, e);
			return false;
		}
		return true;
	}
	
	public int getUserId(String username) {
		String sql="select user_id from user where 1=1 and username='"+username+"'";
		Integer result;
		try{
			result = getJdbcTemplate().queryForInt(sql);
		} catch (EmptyResultDataAccessException ex) {
			result = 0;
		}
		return result;
	}
	
	public Register getUser(int user_id) throws Exception {
		Register user = new Register();
		try {
			String sql = "select t.* from USER t where t.user_id="+user_id;
			user = (Register)this.getJdbcTemplate().queryForObject(sql, new DAORowMapper(Register.class));
		} catch (Exception e) {
			user = null;
		}
		return user;
	}
	
	public void updateUser(RegisterDTO dto) throws Exception {
		String sql = "";
		try {
			sql = "update USER set username='"+StringUtil.filterForSQL(dto.getUsername())+"'," +
					"userpwd='"+StringUtil.filterForSQL(dto.getNewpwd())+"', "+
					"usersex='"+StringUtil.filterForSQL(dto.getUsersex())+"', "+
					"userbirth="+"to_date('" + dto.getUserbirth() + "','yyyy-MM-dd'), "+
					"usertel='"+StringUtil.filterForSQL(dto.getUsertel())+"', "+
					"usermobile='"+StringUtil.filterForSQL(dto.getUsermobile())+"', "+
					"useraddress='"+StringUtil.filterForSQL(dto.getUseraddress())+"', "+
					"usermail='"+StringUtil.filterForSQL(dto.getUsermail())+"', "+
					"usercomp='"+StringUtil.filterForSQL(dto.getUsercomp())+"' "+
					"where user_id="+dto.getUser_id();
			this.getJdbcTemplate().update(sql);
		} catch (Exception e) {
			System.out.println(e);
			log.error("updateState Error! SQL = " + sql);
			
		}
	}
}
